package com.ucode.wap.cgms.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.ucode.cgms.service.CgmsRebateFlowService;
import com.ucode.model.cgms.CgmsRebateFlow;
import com.ucode.model.cgms.convert.CgmsRebateFlowConvert;
import com.ucode.model.cgms.enums.CgmsRebateStatusEnum;
import com.ucode.model.cgms.enums.CgmsRebateTypeEnum;
import com.ucode.model.cgms.vo.CgmsRebateFlowVO;
import com.ucode.tool.annotation.NeedLogin;
import com.ucode.tool.base.Paginator;
import com.ucode.tool.base.ResponseResult;
import com.ucode.tool.base.enums.GlobalResultCodeEnum;
import com.ucode.tool.exception.UcodeServiceException;
import com.ucode.wap.base.core.CurrentLoginUserHandler;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 返点流水移动端接口
 * @Author: Claude
 * @Date: 2023/05/26 11:30
 * @Version: 1.0
 */
@Tag(name = "移动端 - 陪玩社区系统 - 返点流水")
@RestController
@RequestMapping("/api/cgms/rebate-flow")
@Validated
public class ApiCgmsRebateFlowController {

    @Resource
    private CgmsRebateFlowService cgmsRebateFlowService;
    
    @Resource
    private CurrentLoginUserHandler currentLoginUserHandler;

    @GetMapping("/page-list")
    @Operation(summary = "获得分页列表")
    @NeedLogin
    public ResponseResult<Paginator<CgmsRebateFlowVO>> pageList(
            @Schema(description = "返点类型(1-陪玩订单返点 2-俱乐部返点)") 
            @RequestParam(value = "rebateType", required = false) Integer rebateType,
            @Schema(description = "状态(0-待结算 1-已结算 2-已取消)") 
            @RequestParam(value = "statuz", required = false) Integer statuz,
            @Schema(description = "开始时间") @RequestParam(value = "startTime", required = false) LocalDateTime startTime,
            @Schema(description = "结束时间") @RequestParam(value = "endTime", required = false) LocalDateTime endTime,
            @Schema(description = "页码", example="1") @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @Schema(description = "每页记录数", example="10") @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        
        // 获取当前登录用户
        Long userId = currentLoginUserHandler.getCurrentUserId();
        
        Map<String,Object> condition = new HashMap<>();
        condition.put("userId", userId);
        condition.put("rebateType", rebateType);
        condition.put("statuz", statuz);
        condition.put("startTime", startTime);
        condition.put("endTime", endTime);
        condition.put("deltag", false);
        
        Paginator<CgmsRebateFlow> paginator = cgmsRebateFlowService.queryPageForList(condition, pageSize, pageNum);
        Paginator<CgmsRebateFlowVO> voPage = CgmsRebateFlowConvert.INSTANCE.convertPage(paginator);
        
        // 设置枚举名称
        if (CollUtil.isNotEmpty(voPage.getList())) {
            voPage.getList().forEach(vo -> {
                vo.setRebateTypeName(vo.getRebateType() != null ? 
                        CgmsRebateTypeEnum.getNameByCode(vo.getRebateType()) : null);
                vo.setStatuzName(vo.getStatuz() != null ? 
                        CgmsRebateStatusEnum.getNameByCode(vo.getStatuz()) : null);
            });
        }
        
        return ResponseResult.success(voPage);
    }

    @GetMapping("/get/{id}")
    @Parameter(description="主键ID", name = "id", required = true, in = ParameterIn.PATH, example = "1024")
    @Operation(summary = "主键获得信息")
    @NeedLogin
    public ResponseResult<CgmsRebateFlowVO> getById(@PathVariable(name="id", required=true) Long id) {
        // 获取当前登录用户
        Long userId = currentLoginUserHandler.getCurrentUserId();
        
        CgmsRebateFlow bean = cgmsRebateFlowService.validate(id);
        
        // 验证是否为当前用户的返点流水
        if (!userId.equals(bean.getUserId())) {
            throw new UcodeServiceException(GlobalResultCodeEnum.VALIDATE_FAILED);
        }
        
        CgmsRebateFlowVO vo = CgmsRebateFlowConvert.INSTANCE.convert(bean);
        
        // 设置枚举名称
        vo.setRebateTypeName(vo.getRebateType() != null ? 
                CgmsRebateTypeEnum.getNameByCode(vo.getRebateType()) : null);
        vo.setStatuzName(vo.getStatuz() != null ? 
                CgmsRebateStatusEnum.getNameByCode(vo.getStatuz()) : null);
                
        return ResponseResult.success(vo);
    }
} 